package cn.aj.user.service.impl;

import cn.aj.basic.constant.EmailCodeConstant;
import cn.aj.basic.constant.SmsCodeConstant;
import cn.aj.basic.exception.BussinessException;
import cn.aj.basic.tdo.EmailCodeDto;
import cn.aj.user.domain.Logininfo;
import cn.aj.user.domain.User;
import cn.aj.user.dto.EmailRegisterDto;
import cn.aj.user.dto.PhoneRegisterDto;
import cn.aj.user.mapper.LogininfoMapper;
import cn.aj.user.mapper.UserMapper;
import cn.aj.user.service.IUserService;
import cn.aj.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aj
 * @since 2022-11-24
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    /**
    * @Title: phoneRegister
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/24 0024 19:45
    * @Parameters: [registerDto]
    * @Return void
     * 手机注册
    */
    @Override
    public void phoneRegister(PhoneRegisterDto phoneRegisterDto) {
        // 判断参数非空
        // 判断短信验证码非空
        // 先获取redis中的短信验证码,根据k去拿
        String redisMsgCode = (String) redisTemplate.opsForValue().get(String.format(SmsCodeConstant.PHONE_KEY, phoneRegisterDto.getPhone()));
        // 判断存在redis中的验证码非空
        // 拿redis中的短信验证码和phoneRegisterDto进行比较
        if(StringUtils.isBlank(redisMsgCode) || !redisMsgCode.equals(phoneRegisterDto.getMsgCode())) {
            throw new BussinessException("您输入的手机短信验证错误!");
        }
        // 输入的第一次密码校验
        String password = phoneRegisterDto.getPassword();
        if(!password.equals(phoneRegisterDto.getPasswordRepeat())) {
            throw new BussinessException("您输入的2次密码的不一致!");
        }
        // 手机是否注册
        User user = userMapper.loadPhone(phoneRegisterDto.getPhone());
        if (null != user) {
            throw new BussinessException("您的手机号已经注册过!");
        }
        // 添加数据
        // 先保存logininfo的数据
        user = phoneRegisterDto.getUser();
        // 用bean把user转logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        //设置值
        logininfo.setDisable(1);
        logininfo.setType(1);
        logininfoMapper.add(logininfo);
        // 保存user
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);
    }

    /**
    * @Title: emailRegister
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/25 0025 12:31
    * @Parameters: [emailRegisterDto]
    * @Return void
     * 邮箱注册业务
    */
    @Override
    public void emailRegister(EmailRegisterDto emailRegisterDto) {
        String redisEmailMsgCode = (String) redisTemplate.opsForValue().get(String.format(EmailCodeConstant.EMAIL_KEY, emailRegisterDto.getEmail()));
        if(StringUtils.isBlank(redisEmailMsgCode) || !redisEmailMsgCode.equals(emailRegisterDto.getEmailMsgCode())) {
            throw new BussinessException("您输入的邮件验证错误!");
        }
        String password = emailRegisterDto.getPassword();
        if(!password.equals(emailRegisterDto.getPasswordRepeat())) {
            throw new BussinessException("您输入的2次密码错误!");
        }
        User userEmail = userMapper.loadEmail(emailRegisterDto.getEmail());
        if(userEmail != null) {
            throw new BussinessException("您的邮箱已经注册过!");
        }
        // 保存数据
        userEmail = emailRegisterDto.getUser(); // 获取user
        Logininfo logininfoEmail = new Logininfo();
        BeanUtils.copyProperties(userEmail, logininfoEmail); // user转logininfo
        logininfoEmail.setDisable(1);
        logininfoEmail.setType(1);
        logininfoMapper.add(logininfoEmail); // 添加logininfo
        // 保存user
        userEmail.setLogininfoId(logininfoEmail.getId());
        userMapper.add(userEmail);
    }
}
